package src.main.java.com.pms.service;

import src.main.java.com.pms.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class SaleService {

    /**
     * 设置宠物为寄卖状态
     */
    public void setPetForSale(int petId, double askingPrice, String sellerContact) {
        String sql = "UPDATE Pets SET is_for_sale = 1, asking_price = ?, seller_contact = ? WHERE id = ? AND deleted = 0";
        try {
            int result = JDBCUtil.executeUpdateWithParams(sql, askingPrice, sellerContact, petId);
            if (result > 0) {
                System.out.println("宠物已成功设置为寄卖状态！");
            } else {
                System.out.println("设置寄卖状态失败，可能该宠物不存在或已被删除。");
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("设置寄卖状态时发生错误: " + e.getMessage());
        }
    }


    /**
     * 查看所有寄卖中的宠物
     */
    public ResultSet viewPetsForSale() {
        String sql = "SELECT * FROM Pets WHERE is_for_sale = 1 AND deleted = 0";
        try {
            return JDBCUtil.executeQueryWithConnection(sql);
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("查看寄卖宠物时发生错误: " + e.getMessage());
            return null;
        }
    }

    /**
     * 取消寄卖状态
     */
    public boolean cancelPetSale(int petId) {
        String sql = "UPDATE Pets SET is_for_sale = 0, asking_price = NULL, seller_contact = NULL WHERE id = ? AND deleted = 0";
        try {
            int result = JDBCUtil.executeUpdateWithParams(sql, petId);
            if (result > 0) {
                System.out.println("宠物寄卖状态已成功取消！");
                return true;
            } else {
                System.out.println("取消寄卖状态失败，可能该宠物不存在或已被删除。");
                return false;
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("取消寄卖状态时发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 更新卖家联系方式
     *
     * @param petId 宠物ID
     * @param contact 卖家联系方式
     * @return 操作是否成功
     */
    public boolean updateSellerContact(int petId, String contact) {
        String sql = "UPDATE Pets SET seller_contact = ? WHERE id = ?";
        try {
            int affectedRows = JDBCUtil.executeUpdateWithParams(sql, contact, petId);
            return affectedRows > 0;
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新寄卖价格
     */
    public void updateSalePrice(int petId, double newPrice) {
        String sql = "UPDATE Pets SET asking_price = ? WHERE id = ? AND is_for_sale = 1 AND deleted = 0";
        try {
            int result = JDBCUtil.executeUpdateWithParams(sql, newPrice, petId);
            if (result > 0) {
                System.out.println("寄卖价格更新成功！");
            } else {
                System.out.println("更新价格失败，可能该宠物不存在或不在寄卖状态。");
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("更新价格时发生错误: " + e.getMessage());
        }
    }

    /**
     * 检查宠物是否在寄卖状态
     */
    private boolean isPetForSale(int petId) {
        String sql = "SELECT is_for_sale FROM Pets WHERE id = ? AND deleted = 0";
        try (ResultSet rs = JDBCUtil.executeQueryWithConnection(sql, petId)) {
            return rs.next() && rs.getInt("is_for_sale") == 1;
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("检查寄卖状态时发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 根据搜索名称查找待售宠物
     *
     * @param searchName 搜索名称
     * @return ResultSet 包含搜索结果
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public ResultSet searchPetsForSale(String searchName) throws SQLException, ClassNotFoundException {
        String sql = "SELECT * FROM pets WHERE name LIKE ?";
        String param = "%" + searchName + "%"; // 使用模糊查询
        return JDBCUtil.executeQueryWithConnection(sql, param);
    }
}
